package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanException;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsZoneTaskTemp;
import net.realsee.domain.wcs.data.bo.WcsZoneTaskTempBo;
import net.realsee.domain.wcs.data.vo.WcsZoneTaskTempVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsZoneTaskTempMapper;
import net.realsee.service.wcs.data.IWcsZoneTaskTempService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 区域模板Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsZoneTaskTempServiceImpl extends ServiceImpl<WcsZoneTaskTempMapper, WcsZoneTaskTemp> implements IWcsZoneTaskTempService {

    private final WcsZoneTaskTempMapper baseMapper;

    /**
     * 查询区域模板
     */
    @Override
    public WcsZoneTaskTempVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询区域模板列表
     */
    @Override
    public TableDataInfo<WcsZoneTaskTempVo> selectVoPage(WcsZoneTaskTempBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsZoneTaskTemp> lqw = buildVoQueryWrapper(bo);
        Page<WcsZoneTaskTempVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询区域模板列表
     */
    @Override
    public List<WcsZoneTaskTempVo> selectVoList(WcsZoneTaskTempBo bo) {
        LambdaQueryWrapper<WcsZoneTaskTemp> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsZoneTaskTemp> buildVoQueryWrapper(WcsZoneTaskTempBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsZoneTaskTemp> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFacility()), WcsZoneTaskTemp::getFacility, bo.getFacility());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkshop()), WcsZoneTaskTemp::getWorkshop, bo.getWorkshop());
        lqw.eq(StringUtils.isNotBlank(bo.getZoneRelationType()), WcsZoneTaskTemp::getZoneRelationType, bo.getZoneRelationType());
        return lqw;
    }

    /**
     * 新增区域模板
     */
    @Override
    public Boolean insertByBo(WcsZoneTaskTempBo bo) {
        if (StringUtils.isNotBlank(bo.getFromZone()) && StringUtils.isNotBlank(bo.getToZone())){
            // 通过起始区域和目的区域校验是否添加过模板
            List<WcsZoneTaskTemp> zoneTaskTempList = baseMapper.getZoneTaslTempListByFromZoneAndToZone(bo);

            if (!ObjectUtils.isEmpty(zoneTaskTempList)){
                throw new BeanException("该起始区域和目的区域已添加模板，请勿重复添加！");
            }
        }

        WcsZoneTaskTemp add = BeanUtil.toBean(bo, WcsZoneTaskTemp.class);
        return this.save(add);
    }

    /**
     * 修改区域模板
     */
    @Override
    public Boolean updateByBo(WcsZoneTaskTempBo bo) {
        WcsZoneTaskTemp update = BeanUtil.toBean(bo, WcsZoneTaskTemp.class);
        return this.updateById(update);
    }

    /**
     * 批量删除区域模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 通过仓库和区域查询模板信息
     */
    @Override
    public WcsZoneTaskTemp getByWarehouseAndZone(String fromWarehouse, String fromZone, String toWarehouse, String toZone) {
        return this.lambdaQuery()
            .eq(WcsZoneTaskTemp::getFromWarehouse, fromWarehouse)
            .eq(WcsZoneTaskTemp::getFromZone, fromZone)
            .eq(WcsZoneTaskTemp::getToWarehouse, toWarehouse)
            .eq(WcsZoneTaskTemp::getToZone, toZone)
            .last("limit 1")
            .one();
    }
}
